package com.ksyun.campus.dataserver.controller;

import com.ksyun.campus.dataserver.domain.ApiResponse;
import com.ksyun.campus.dataserver.domain.dto.ReadDTO;
import com.ksyun.campus.dataserver.domain.dto.WriteContentDTO;
import com.ksyun.campus.dataserver.services.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

@RestController("/")
public class DataController {

    @Autowired
    DataService dataService;
    /**
     * 1、读取request content内容并保存在本地磁盘下的文件内
     * 2、同步调用其他ds服务的write，完成另外2副本的写入
     * 3、返回写成功的结果及三副本的位置
     */
    @PostMapping("write")
    public ApiResponse writeFile(@RequestBody WriteContentDTO writeContentDTO) {
        return dataService.write(writeContentDTO);
    }


    /**
     * 在指定本地磁盘路径下，读取指定大小的内容后返回
     * @return
     */
    @PostMapping("read")
    public byte[] readFile(@RequestBody ReadDTO readDTO){
        byte[] read = dataService.read(readDTO);
        return read;
    }

    @GetMapping("delete")
    public ApiResponse deleteFile(@RequestParam String path){
        return dataService.deleteFile(path);
    }

    /**
     * 关闭退出进程
     */
    @RequestMapping("shutdown")
    public void shutdownServer(){
        System.exit(-1);
    }

}
